<HTML><HEAD>
<!--
    -----------
    Spreadsheet
    -----------
-->

<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)1998 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/

// --------------------Spreadsheet Functions---------------------

var sheetWidth = 6
var sheetHeight = 6

// Recalculate after a change is detected
function recalculate()
{
    var s = 0
    
    // Row sums
    for (var j = 1; j < (sheetHeight -1); j++)
    {
        var sum = 0
        for (var i = 1; i < (sheetWidth-1); i++) 
        {
            var tmpResult = parseInt(eval("document.forms[0].CELL"+i+"X"+j+".value"));
            if(isNaN(tmpResult)
                tmpResult = 0;
            sum += tmpResult;
        }
        eval("document.forms[0].CELL"+(sheetWidth-1)+"X"+j+".value = "+sum)
    }

    // Column sums
    for (var i = 1; i < (sheetWidth); i++)
    {
        var sum = 0
        for (var j = 1; j < (sheetHeight-1); j++) 
        {
            sum += parseInt(eval("document.forms[0].CELL"+i+"X"+j+".value"))
        }
        eval("document.forms[0].CELL"+i+"X"+(sheetHeight-1)+".value = "+sum)
    }
}

// Create the Spreadsheet
function startSheet()
{
    document.write("<FORM><TABLE>")
    for (var j = 0; j < sheetHeight; j++)
    {
        document.write("<TR>")
        for (var i = 0; i < sheetWidth; i++)
        {
            document.write("<TD ALIGN=CENTER>")
            document.write("<INPUT TYPE='TEXT' SIZE=6 "+
                " NAME = 'CELL"+i+"X"+j+"' ")                
            if ((i == 0) || (j == 0)) document.write(" VALUE='Text' ")
            else document.write(" VALUE='0' ")
            document.write(" onChange='recalculate()'></TD>")
        }
        document.write("</TR>")
    }
    document.write("</TABLE></FORM>")
}

<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff">
    
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
ALIGN = CENTER>Spreadsheet</H1></FONT>

<BLOCKQUOTE><FONT COLOR="770000">
    Use this very simple JavaScript spreadsheet to help with
    your sums. Enter numbers
    in the middle cells and they will be summed to the sides
    and bottoms of the sheet. After you type in each cell,
    press the tab key or click in another cell to activate
    the "changed" property. <b>Be patient!</b> It may take a second 
    to load.  
</FONT></BLOCKQUOTE>
    
<SCRIPT>
// Create a New Spreadsheet
    startSheet()
</SCRIPT>
        
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
    This Spreadsheet shows highlights JavaScript's flexibility.
    JavaScript itself generates the spreadsheet from the
    two variables <FONT COLOR="770000">sheetHeight</FONT>
    and <FONT COLOR="770000">sheetWidth</FONT>. The entire
    creation function is shown
    below.  Border cells are marked with the string "text" to
    indicate they are not included in calculations. OnChange events
    trigger recalculation.
</FONT>

</FONT><FONT COLOR="770000"><PRE>
// Create the Spreadsheet
function startSheet()
{
    document.write("&lt;FORM&gt;&lt;TABLE&gt;")
    for (var j = 0; j &lt; sheetHeight; j++)
    {
        document.write("&lt;TR&gt;")
        for (var i = 0; i &lt; sheetWidth; i++)
        {
            document.write("&lt;TD ALIGN=CENTER&gt;")
            document.write("&lt;INPUT TYPE='TEXT' SIZE=6 "+
                " NAME = 'CELL"+i+"X"+j+"' ")                
            if ((i == 0) || (j == 0)) document.write(" VALUE='Text' ")
            else document.write(" VALUE='0' ")
            document.write(" onChange='recalculate()'></TD>")
        }
        document.write("&lt;/TR&gt;")
    }
    document.write("&lt;/TABLE&gt;&lt;/FORM&gt;")
}
</PRE></FONT>

</FONT></BLOCKQUOTE>

<h5>Copyright &copy;1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>